using System;
using System.Collections.Generic;
using System.Text;
using System.Management.Automation;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;


namespace Community.SharePoint.Commands
{
    [Cmdlet(VerbsCommon.New, "Web", SupportsShouldProcess = false)]
    public class NewWebCommand : Cmdlet
    {
        internal string _url;
        internal string _title;
        internal string _description;
        internal uint _LCID = 0;
        internal string _webTemplate;
        internal bool _uniquePermissions;
        internal bool _convert;
        

        [Parameter(Position = 0,
                Mandatory = true,
                ValueFromPipelineByPropertyName = true)]
        public String Url
        {
            get { return _url; }
            set
            {
                _url = value;
            }
        }
        [Parameter( Mandatory = false,
               ValueFromPipelineByPropertyName = true)]
        public String Title
        {
            get { return _title; }
            set
            {
                _title = value;
            }
        }

        [Parameter(Mandatory = false,
               ValueFromPipelineByPropertyName = true)]
        public String Description
        {
            get { return _description; }
            set
            {
                _description = value;
            }
        }

        [Parameter]
        public Int32  LCID
        {
            get { return (Int32)_LCID; }
            set
            {
                _LCID = (uint)value;
            }
        }

        [Parameter(Mandatory = false,
              ValueFromPipelineByPropertyName = true)]
        public String Template
        {
            get { return _webTemplate; }
            set
            {
                _webTemplate = value;
            }
        }
        [Parameter]
        public SwitchParameter UniquePermissions
        {
            get
            {
                return this._uniquePermissions ;
            }
            set
            {
                this._uniquePermissions = (bool)value;
            }
        }

        [Parameter]
        public SwitchParameter Convert
        {
            get
            {
                return this._convert ;
            }
            set
            {
                this._convert = (bool)value;
            }
        }


        internal string GetServerRelUrlFromFullUrl(string strUrl)
        {
            int index = strUrl.IndexOf("//");
            if ((index < 0) || (index == (strUrl.Length - 2)))
            {
                throw new ArgumentException();
            }
            int startIndex = strUrl.IndexOf('/', index + 2);
            if (startIndex < 0)
            {
                return "/";
            }
            string str = strUrl.Substring(startIndex);
            if ((str.Length > 1) && (str[str.Length - 1] == '/'))
            {
                return str.Substring(0, str.Length - 1);
            }
            return str;
        }



        protected override void ProcessRecord()
        {
            using (SPSiteAdministration siteAdmin = new SPSiteAdministration(_url))
            {
                string refUrl = GetServerRelUrlFromFullUrl(_url);
              //  WriteProgress(string.Format("Creating Web: {0}", _url));
                WriteDebug(string.Format("Creating Web: {0}", _url));
 
                siteAdmin.AddWeb(refUrl, _title, _description, _LCID, _webTemplate, _uniquePermissions, _convert);
            }

            using (SPSite site = new SPSite(_url))
            {
                WriteObject(site.OpenWeb());
            }
        }
    }
}
